﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;


namespace BranchOfSansar.Register.Products
{
    public partial class frmRegEquipment : Form
    {


        private Equipment CurrEquip = null;
        private bool IsNew = false;
        public frmRegEquipment()
        {
            InitializeComponent();
            IsNew = true;
            CurrEquip = new Equipment();

            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient reg = new BranchOfSansar.Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                cmbUnit.Properties.Items.AddRange(reg.Units().Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { Display = s.UnitName, StrValue = s.UnitName }).ToArray());
                cmbUnit.SelectedIndex = 0;
                cmbCompany.Properties.Items.AddRange(reg.CompanyList().Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { Display = s.Name, StrValue = s.CompanyNo }).ToArray());
                
                cmbCompany.SelectedIndex = 0;
                cmbStatus.SelectedIndex = 1;
            }
        }

        public frmRegEquipment(Equipment eqp)
            : this()
        {
            IsNew = false;
            CurrEquip = eqp;
            txtCode.Text = eqp.EquipCode.ToString();
            txtName.Text = eqp.Name;
            txtPrice.Text = eqp.Price.ToString("#,##0");
            cmbUnit.Text = eqp.Unit;            
            Globals.setComboSelectedItem(cmbCompany, eqp.CompanyNo);
            cmbStatus.SelectedIndex = eqp.Status ? 1 : 0;

        }



        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!Globals.DoValidation(pnlMain))
                return ;

            using (RegisterClient reg = new RegisterClient(Globals.Token))
            {
                CurrEquip = new Equipment()
                {
                    CompanyNo = ((BranchOfSansar.Sansar.Enquiry.ComboItem)cmbCompany.SelectedItem).StrValue,
                    Price = txtPrice.Value,
                    Unit = cmbUnit.SelectedItem.ToString(),
                    EquipCode = int.Parse(txtCode.Text),
                    Name = txtName.Text,                     
                    Status = cmbStatus.SelectedIndex == 1
                };
                Result res;
                if (IsNew)
                    res=reg.AddEquipment(CurrEquip);
                else
                    res=reg.EditEquipment(CurrEquip);

                if (res.Code == 0)
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                else
                {
                    Globals.Err(res.Message);
                }
            }

        }

        private void frmRegEquipment_Load(object sender, EventArgs e)
        {

        }

        
    }
}
